package testes.vSys.action;

import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import testes.vSys.bean.Brand;
import testes.vSys.bean.Model;
import testes.vSys.bean.Vehicle;
import testes.vSys.dao.DAOFactory;
import testes.vSys.dao.IModelDao;
import testes.vSys.form.ModelRecordForm;
import testes.vSys.util.Utilities;

public class ModelRecordAction extends DispatchAction{

	private final String BRANDS_COLLECTION = "brandsCollection";
	private final String MODELS_COLLECTION = "modelsCollection";
	private final String MESSAGE = "messageErrorEx";
	
	private static IModelDao modelDAO;
	
	static {
		modelDAO = DAOFactory.getModelDao();
	}
	
	public ActionForward load(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelRecordForm modelForm = (ModelRecordForm)form;		
		
		Collection<Brand> brands = modelDAO.getAvaiableBrands();		
		request.setAttribute(BRANDS_COLLECTION, brands);

		Collection<Model> models = new ArrayList<Model>();
		Brand brand = null; 
		if (Utilities.validateNumeric(modelForm.getSelectedBrand()))
			brand = modelDAO.getBrandById(Integer.valueOf(modelForm.getSelectedBrand()));

		if (brand == null) {
			if (brands != null) {
				if (brands.iterator().hasNext())
					brand = brands.iterator().next();
			}
		}

		if (brand != null) {
			models = modelDAO.getModelsByBrand(brand);
			modelForm.setSelectedBrand(String.valueOf(brand.getId()));
		}
		
		request.setAttribute(MODELS_COLLECTION, models);

		return mapping.findForward("modelRecord");
	}
	
	public ActionForward insertBrand(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelRecordForm modelForm = (ModelRecordForm)form;
		
		System.out.println(modelForm.getOpr());
		
		Brand brand = new Brand(modelForm.getBrand());
		modelDAO.createBrand(brand);
		
		return load(mapping, form, request, response);
	}
	
	public ActionForward insertModel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelRecordForm modelForm = (ModelRecordForm)form;
		
		System.out.println(modelForm.getOpr());
		

		Brand brand = null;
		if (Utilities.validateNumeric(modelForm.getSelectedBrand()))
			brand = modelDAO.getBrandById(Integer.valueOf(modelForm.getSelectedBrand()));
		
		Model model = new Model(brand,null,modelForm.getModel());
		modelDAO.createModel(model);

		return load(mapping, form, request, response);
	}
	

	/*public ActionForward editBrand(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelRecordForm modelForm = (ModelRecordForm)form;
		
		System.out.println(modelForm.getOpr());

		//TODO
	
		return load(mapping, form, request, response);
	}

	public ActionForward editModel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelRecordForm modelForm = (ModelRecordForm)form;
		
		System.out.println(modelForm.getOpr());

		//TODO

		return load(mapping, form, request, response);
	}*/
	
	public ActionForward removeBrand(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelRecordForm modelForm = (ModelRecordForm)form;
		
		System.out.println(modelForm.getOpr());
		
		if (Utilities.validateNumeric(modelForm.getId())) {
			int id = Integer.valueOf(modelForm.getId());
			if(!modelDAO.removeBrand(id))
				request.setAttribute(MESSAGE, Utilities.getMessage("vehicle.label.removeFailed"));
		}

		return load(mapping, form, request, response);
	}

	public ActionForward removeModel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelRecordForm modelForm = (ModelRecordForm)form;
		
		System.out.println(modelForm.getOpr());
		
		if (Utilities.validateNumeric(modelForm.getId())) {
			if(!modelDAO.removeModel(Integer.valueOf(modelForm.getId())))
				request.setAttribute(MESSAGE, Utilities.getMessage("vehicle.label.removeFailed"));
		}
		
		return load(mapping, form, request, response);
	}
	
	
	public ActionForward clearModelFields(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		ModelRecordForm modelForm = (ModelRecordForm)form;
		
		System.out.println(modelForm.getOpr());
		
		modelForm.clearModelFields();
		
		return load(mapping, form, request, response);
	}	
	
	
	public ActionForward clearBrandFields(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		ModelRecordForm modelForm = (ModelRecordForm)form;
		
		System.out.println(modelForm.getOpr());
		
		modelForm.clearBrandFields();
		
		return load(mapping, form, request, response);
	}	
	
	public ActionForward changeModel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		ModelRecordForm modelForm = (ModelRecordForm)form;
		
		System.out.println(modelForm.getOpr());
		
		return load(mapping, form, request, response);
	}

}
